<?php

namespace App\Http\Controllers\Admin;

use App\Models\Order;
use App\Models\OrderGoods;
use App\Models\Picture;
use App\Models\Shop;
use App\Models\Worker;


/**
 * 订单管理控制器
 */
class OrderController extends BaseController
{

    public function __construct(Order $order, Shop $shop, Picture $picture, OrderGoods $orderGoods, Worker $worker)
    {
        parent::__construct();
        $this->order = $order;
        $this->shop = $shop;
        $this->picture = $picture;
        $this->orderGoods = $orderGoods;
        $this->worker = $worker;
    }


    public function getIndex()
    {
        $shopList = $this->shop->_getList(['admin_id' => is_login()])->toArray();
        $rows = $this->order->getOwnOrderList(request('shop_id'));
        if (request('shop_id')) {
            foreach ($shopList as $v) {
                if ($v['id'] == request('shop_id')) {
                    $curentShop = $v;
                }
            }
        }
        $ext = $this->picture->config['saveExt'];
        $mini = 60;
        if ($this->picture->config['cut_Mini_Size']) {
            $miniArr = explode(',', $this->picture->config['cut_Mini_Size']);
            $mini = $miniArr[0];
        }
        $listType = $this->order->listType;
        return view('admin.order.index', compact('rows', 'shopList', 'curentShop', 'ext', 'mini','listType'));
    }

    public function getInfo()
    {

    }

    public function getCancle()
    {
        if (request('type')) {
            $res = $this->order->cancel();
            if (!$res) {
                return $this->error($this->order->getError() ? $this->order->getError() : '未知错误');
            }
            return $this->success($res);
        }
        return view('admin.order.cancle');
    }

    public function getDeleteOrder()
    {
        $res = $this->order->deleteOrder();
        if (!$res) {
            return $this->error($this->order->getError() ? $this->order->getError() : '未知错误');
        }
        return $this->success($res);
    }

    public function getSend()
    {
        if (request('type')) {
            $res = $this->order->send();
            if (!$res) {
                return $this->error($this->order->getError() ? $this->order->getError() : '未知错误');
            }
            return $this->success($res);
        }
        $worker_id = request('worker_id');
        $rows = $this->worker->_getList(['shop_id' => request('shop_id'), 'status' => 1], 'id,name,code')->toArray();
        return view('admin.order.send', compact('rows', 'worker_id'));
    }

}
